//导入
const mongoose = require('mongoose');

//连接
mongoose.connect('mongodb://127.0.0.1:27017/games');

//绑定回调
mongoose.connection.on('open', () => {
  //创建结构对象
  const MovieSchema = new mongoose.Schema({
    name: {
      type: String,
      required: true
    },
    director: {
      type: String,
      required: true
    },
    actors: {
      type: Array
    },
    rate: {
      type: Number,
      default: 0
    },
    pub_time: {
      type: Date
    }
  })

  //创建模型对象
  const MovieModel = mongoose.model('movie', MovieSchema);

  //插入电影
  // MovieModel.insertMany([
  //   {
  //     name: "夏洛特烦恼",
  //     director: "开心麻花",
  //     actors: ['沈腾','玛丽','尹正'],
  //     rate: 9.5,
  //     pub_time: new Date(2012, 1, 15)
  //   }
  // ], (err, data) => {
  //   if(err) throw err;
  //   console.log(data);
  //   // mongoose.connection.close();
  //   mongoose.disconnect();
  // })

  //删除操作
  // MovieModel.deleteOne({_id: '64390cf9b59926f063b5dfff'}, (err, data) => {
  //   if(err) throw err;
  //   console.log(data);
  //   mongoose.disconnect();
  // })

  //批量删除
  MovieModel.deleteMany({director: '张翰戈'}, (err, data) => {
    if(err) throw err;
    console.log(data);
    mongoose.disconnect();
  })

});


mongoose.connection.on('error', () => {
  console.log('连接失败~~');
});